﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpUserControls.UserControls
{
    public partial class ToggleSwitch : UserControl
    {
        public ToggleSwitch()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(ToggleSwitch_Loaded);
            this.LayoutRoot.Tap += new EventHandler<GestureEventArgs>(LayoutRoot_Tap);
        }

        void LayoutRoot_Tap(object sender, GestureEventArgs e)
        {
            this.Value = this.Value == ToggleSwitchValue.ON ? ToggleSwitchValue.OFF : ToggleSwitchValue.ON;

            if (ToggleSwitched != null) ToggleSwitched(this, EventArgs.Empty);
        }

        void ToggleSwitch_Loaded(object sender, RoutedEventArgs e)
        {
            RenderButtons();
        }

        private ToggleSwitchValue _toggleSwitchValue = ToggleSwitchValue.OFF;
        [Browsable(true)]
        public ToggleSwitchValue Value 
        {
            get
            {
                return _toggleSwitchValue;
            }
            set
            {
                _toggleSwitchValue = value;
                RenderButtons();
            }
        }

        public event EventHandler ToggleSwitched;

        private Size _buttonSize = new Size(66, 32);
        [Browsable(true)]
        public Size ButtonSize 
        {
            get
            {
                return _buttonSize;
            }
            set
            {
                _buttonSize = value;
                RenderButtons();
            }
        }

        private Size _selectedButtonSize = new Size(70, 36);
        [Browsable(true)]
        public Size SelectedButtonSize 
        {
            get
            {
                return _selectedButtonSize;
            }
            set
            {
                _selectedButtonSize = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public Brush Backgroud
        {
            get
            {
                return LayoutBorder.Background;
            }
            set
            {
                LayoutBorder.Background = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public new Brush BorderBrush
        {
            get
            {
                return LayoutBorder.BorderBrush;
            }
            set
            {
                LayoutBorder.BorderBrush = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public new Thickness BorderThickness
        {
            get
            {
                return LayoutBorder.BorderThickness;
            }
            set
            {
                LayoutBorder.BorderThickness = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public string LeftText
        {
            get
            {
                return LeftTextBlock.Text;
            }
            set
            {
                LeftTextBlock.Text = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public string RightText
        {
            get
            {
                return RightTextBlock.Text;
            }
            set
            {
                RightTextBlock.Text = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public Brush ButtonBackground
        {
            get
            {
                return LeftButton.Background;
            }
            set
            {
                LeftButton.Background = RightButton.Background = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public Brush ButtonBorderBrush
        {
            get
            {
                return LeftButton.BorderBrush;
            }
            set
            {
                LeftButton.BorderBrush = RightButton.BorderBrush = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public Thickness ButtonBorderThickness
        {
            get
            {
                return LeftButton.BorderThickness;
            }
            set
            {
                LeftButton.BorderThickness = RightButton.BorderThickness = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public Brush SelectedButtonBackground
        {
            get
            {
                return SelectedButton.Background;
            }
            set
            {
                SelectedButton.Background = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public Brush SelectedButtonBorderBrush
        {
            get
            {
                return SelectedButton.BorderBrush;
            }
            set
            {
                SelectedButton.BorderBrush = value;
                RenderButtons();
            }
        }

        [Browsable(true)]
        public Thickness SelectedButtonBorderThickness
        {
            get
            {
                return SelectedButton.BorderThickness;
            }
            set
            {
                SelectedButton.BorderThickness = value;
                RenderButtons();
            }
        }


        private void RenderButtons()
        {
            SelectedButton.Width = SelectedButtonSize.Width;
            SelectedButton.Height = SelectedButtonSize.Height;

            LeftButton.Width = RightButton.Width = ButtonSize.Width;
            LeftButton.Height = RightButton.Height = ButtonSize.Height;


            if (Value == ToggleSwitchValue.ON)
            {
                SelectedText.Text = LeftTextBlock.Text;
                SelectedButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            }
            else
            {
                SelectedText.Text = RightTextBlock.Text;
                SelectedButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            }
        }
    }

    public enum ToggleSwitchValue
    {
        ON,
        OFF,
    }
}
